﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Threading;

using PickGold.Callers;
using PickGold.Handler;

namespace PickGold.Arguments
{
	/// <summary>
	/// 异步包装器，主要用于附加额外的
	/// </summary>
	[Serializable]
	[ComVisible(true)]
	[ClassInterface(ClassInterfaceType.AutoDispatch)]
	public class AsyncResultEventArgs : EventArgs, IAsyncResultWrap, IAsyncResult
	{
		/// <summary>
		/// 异步操作状态包装器
		/// </summary>
		public AsyncResultWrap ResultWrap;

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="maxStep">默认为一次异步</param>
		public AsyncResultEventArgs(int maxStep) : this(maxStep, null, null, null) { }

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="maxStep">默认为一次异步</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		public AsyncResultEventArgs(int maxStep, IAsyncResult asyncResult) : this(maxStep, asyncResult, null, null) { }

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="maxStep">默认为一次异步</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public AsyncResultEventArgs(int maxStep, AsyncCallback asyncCallback, object asyncState) : this(maxStep, null, asyncCallback, asyncState) { }

		/// <summary>
		/// 使用一个异步操作状态构造一个异步操作包装器实例
		/// </summary>
		/// <param name="maxStep">默认为一次异步</param>
		/// <param name="asyncResult">需要包装的异步操作状态</param>
		/// <param name="asyncCallback">异步操作回调委托</param>
		/// <param name="asyncState">附加的异步操作状态的用户定义对象</param>
		public AsyncResultEventArgs(int maxStep, IAsyncResult asyncResult, AsyncCallback asyncCallback, object asyncState)
		{
			this.ResultWrap = new AsyncResultWrap(maxStep, asyncResult, asyncCallback, asyncState);
		}

		#region IAsyncResult

		object IAsyncResult.AsyncState
		{
			get
			{
				return this.ResultWrap.AsyncState;
			}
		}

		WaitHandle IAsyncResult.AsyncWaitHandle
		{
			get
			{
				return this.ResultWrap.AsyncWaitHandle;
			}
		}

		bool IAsyncResult.CompletedSynchronously
		{
			get
			{
				return this.ResultWrap.CompletedSynchronously;
			}
		}

		bool IAsyncResult.IsCompleted
		{
			get
			{
				return this.ResultWrap.IsCompleted;
			}
		}

		#endregion

		#region IAsyncResultWrap

		object IAsyncResultWrap.WrapTag
		{
			set
			{
				this.ResultWrap.WrapTag = value;
			}
			get
			{
				return this.ResultWrap.WrapTag;
			}
		}
		object IAsyncResultWrap.WrapReturn
		{
			set
			{
				this.ResultWrap.WrapReturn = value;
			}
			get
			{
				return this.ResultWrap.WrapReturn;
			}
		}
		object[] IAsyncResultWrap.WrapOutput
		{
			set
			{
				this.ResultWrap.WrapOutput = value;
			}
			get
			{
				return this.ResultWrap.WrapOutput;
			}
		}
		Exception IAsyncResultWrap.WrapError
		{
			set
			{
				this.ResultWrap.WrapError = value;
			}
			get
			{
				return this.ResultWrap.WrapError;
			}
		}
		IAsyncResult IAsyncResultWrap.WrapResult
		{
			set
			{
				this.ResultWrap.WrapResult = value;
			}
			get
			{
				return this.ResultWrap.WrapResult;
			}
		}
		AsyncCallback IAsyncResultWrap.WrapCallback
		{
			set
			{
				this.ResultWrap.WrapCallback = value;
			}
			get
			{
				return this.ResultWrap.WrapCallback;
			}
		}
		Delegate IAsyncResultWrap.WrapEnd
		{
			set
			{
				this.ResultWrap.WrapEnd = value;
			}
			get
			{
				return this.ResultWrap.WrapEnd;
			}
		}

		IAsyncResultWrap IAsyncResultWrap.Complete()
		{
			return this.ResultWrap.Complete();
		}

		bool IAsyncResultWrap.WaitOne()
		{
			return this.ResultWrap.WaitOne();
		}

		#endregion
	}
}
